package com.ruantu.clothing.service.impl;


import com.ruantu.clothing.dao.VipMapper;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.Vip;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruantu.clothing.service.VipService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class VipServiceImpl implements VipService {

    @Autowired
    private VipMapper vipMapper;

    @Override
    public List<Vip> findList(Vip vip, PageBean pageBean) {
        Map<String, Object> map = new HashMap();
        map.put("jgId", vip.getJgid());
        if (StringUtils.isNotBlank(vip.getHuiyuanming())) {
            map.put("huiyuanming", vip.getHuiyuanming());
        }
        if (StringUtils.isNotBlank(vip.getPhone())) {
            map.put("phone", vip.getPhone());
        }
        map.put("state", vip.getState());
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        pageBean.setTotal(vipMapper.findVipListCount(map));
        return vipMapper.findVipList(map);
    }

    @Override
    public Vip findVipById(Vip vip) {
        return vipMapper.findVipById(vip);
    }

    @Override
    @Transactional
    public int addMoney(Vip vip) {
        int code = vipMapper.updateBlanceById(vip);
        if (code <= 0) {
            throw new MyException("数据异常，请刷新重新尝试");
        }
        vip.setCzmoney(vip.getBalance());
        //增加充值记录
        code = vipMapper.insertRecord(vip);
        if (code <= 0) {
            throw new MyException("数据异常，请刷新重新尝试");
        }
        return code;
    }

    @Override
    @Transactional
    public int saveVip(Vip vip) {
        if (StringUtils.isNotBlank(vip.getId())) {//修改
            int code = vipMapper.updateVip(vip);
            if (code <= 0) {
                throw new MyException("保存失败，请检查数据重新尝试");
            }
        } else {//新增
            vip.setId(UUIDUtil.getUUID());
            //新增会员
            int code = vipMapper.saveVip(vip);
            if (code <= 0) {
                throw new MyException("保存失败，请检查数据重新尝试");
            }
            vip.setCzmoney(vip.getBalance());
            //新增充值记录
            if (vip.getCzmoney() != null && vip.getCzmoney() > 0) {
                vip.setLeixing(0);
                code = vipMapper.insertRecord(vip);
            }
        }
        return 1;
    }

    @Override
    public Vip findVipWithXiaoPiao(String id) {
        return vipMapper.findVipWithXiaopiao(id);
    }

    @Override
    public int updateVipById(Vip vip) {
        int code = vipMapper.updateVipById(vip);
        if (code <= 0) {
            throw new MyException("操作失败，请刷新并重新尝试");
        }
        return code;
    }

    @Override
    public List<Vip> findChongzhiji(Vip vip, PageBean pageBean, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(vip.getHuiyuanming())) {
            map.put("huiyuanming", vip.getHuiyuanming());
        }
        if (StringUtils.isNotBlank(vip.getPhone())) {
            map.put("phone", vip.getPhone());
        }
        map.put("startTime", startTime);
        map.put("endTime", endTime + " 23:59:59");
        map.put("jgid", vip.getJgid());
        map.put("start", pageBean.getStart());
        map.put("rows", pageBean.getRows());
        pageBean.setTotal(vipMapper.findChongzhijiCount(map));
        return vipMapper.findChongzhiji(map);
    }
}
